#include "freertos/FreeRTOS.h"
#include "freertos/task.h"

#include "key.h"

/**
 * @brief       BOOT按键初始化程序
 * @param       无
 * @retval      无
 */
void boot_init(void)
{
    gpio_config_t boot_init_struct;
    boot_init_struct.pin_bit_mask = 1ULL << BOOT_GPIO_PIN;
    boot_init_struct.mode = GPIO_MODE_INPUT;
    boot_init_struct.intr_type = GPIO_INTR_DISABLE;
    boot_init_struct.pull_up_en = GPIO_PULLUP_ENABLE;
    boot_init_struct.pull_down_en = GPIO_PULLDOWN_DISABLE;
    gpio_config(&boot_init_struct);
}

/**
 * @brief       BOOT按键扫描程序
 * @param       无
 * @retval      无
 */
uint8_t boot_scan(uint8_t mode)
{
    uint8_t key_value = BOOT_LIFT;
    static uint8_t key_pres = BOOT_LIFT;

    if (mode == 1)
    {
        key_pres = BOOT_LIFT;
    }

    if (BOOT_KEY_VALUE == BOOT_PRES)
    {
        vTaskDelay(pdMS_TO_TICKS(10));
        if (BOOT_KEY_VALUE == BOOT_PRES && key_pres == BOOT_LIFT)
        {
            key_value = BOOT_PRES;
            key_pres = BOOT_PRES;
        }
    }
    else
    {
        key_pres = BOOT_LIFT;
    }
    return key_value;
}